package com.bdqn.tour.servlet.Tj;

import com.bdqn.tour.dao.TicketDao;
import com.bdqn.tour.pojo.Ticket;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;

@WebServlet("/edit-ticket")
public class EditTicketServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private Connection connection;

    @Override
    public void init() throws ServletException {
        super.init();
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/tourproject", "root", "123456");
        } catch (ClassNotFoundException | SQLException e) {
            throw new ServletException(e);
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");

        try {
            TicketDao ticketDAO = new TicketDao(connection);
            Ticket ticket = ticketDAO.getTicketById(id);
            request.setAttribute("ticket", ticket);
            RequestDispatcher dispatcher = request.getRequestDispatcher("/edit_ticket.jsp");
            dispatcher.forward(request, response);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("utf-8");
        String id = request.getParameter("id");
        String type = request.getParameter("type");
        String ticketType = request.getParameter("ticketType");
        String departureCity = request.getParameter("departureCity");
        String arrivalCity = request.getParameter("arrivalCity");
        String departureDateString = request.getParameter("departureDate");
        String startTimeString = request.getParameter("startTime");
        String endTimeString = request.getParameter("endTime");
        double price = Double.parseDouble(request.getParameter("price"));
        boolean hasInfantOption = "on".equals(request.getParameter("hasInfantOption"));
        boolean hasChildOption = "on".equals(request.getParameter("hasChildOption"));

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");

        LocalDate departureDate = null;
        LocalTime startTime = null;
        LocalTime endTime = null;

        try {
            departureDate = dateFormat.parse(departureDateString).toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            if (startTimeString != null && !startTimeString.trim().isEmpty()) {
                startTime = LocalTime.parse(startTimeString);
            }
            if (endTimeString != null && !endTimeString.trim().isEmpty()) {
                endTime = LocalTime.parse(endTimeString);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Ticket ticket = new Ticket(id, type,ticketType, departureCity, arrivalCity, departureDate, startTime, endTime, price, hasInfantOption, hasChildOption);

        try {
            TicketDao ticketDAO = new TicketDao(connection);
            ticketDAO.updateTicket(ticket);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        response.sendRedirect("view-tickets");
    }

    @Override
    public void destroy() {
        try {
            if (connection != null && !connection.isClosed()) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
